<?php
namespace app\home\controller;

use app\BaseController;
use app\home\model\Article;
use app\home\model\Banner;

use app\home\model\Supply;
use app\home\model\Demand;
use app\home\model\News;
use app\home\model\Seo;
use app\home\model\ShortLink;
use app\api\model\Usage;


use think\facade\View;

class Index extends BaseController
{

    public function index()
    { 
        $hash = $_SERVER['QUERY_STRING'];
        $reg = "/^[a-zA-Z0-9]{6}$/";
        if($hash != null ){
            if(preg_match($reg, $hash)){
                //短链接
                $obj = ShortLink::where(array('hash_str'=>$hash))->find();
                
                if($obj != null){
                    $obj['c_name'] = str_replace("Action", '', $obj['c_name']);

                    $usage = new Usage;
                    $usage->save([
                        'access_page'=>$obj['c_name'],
                        'access_time'=>date('Y-m-d', time()),
                        'access_ip'=>$_SERVER["REMOTE_ADDR"]
                    ]);


                    $col = invoke("app\\home\\controller\\".$obj['c_name']);
                    $_GET[$obj['p_name']] = $obj['p_id'];
                    return $col->index();
                }else{
                    abort(404);
                    return;
                }
            }else{
                abort(404);
                return;
            }
        }

        $usage = new Usage;
        $usage->save([
            'access_page'=>'Other',
            'access_time'=>date('Y-m-d', time()),
            'access_ip'=>$_SERVER["REMOTE_ADDR"]
        ]);

        View::assign('kw', Seo::getKeywords());
        View::assign('desc', Seo::getDesc());
        View::assign('news', News::getByOrder('date_desc'));
        View::assign('article', Article::getByOrder('date_desc', 30));
        View::assign('demand', Demand::getByOrder("date_desc"));
        View::assign('supply', Supply::getByOrder("date_desc"));
        
        View::assign('banner', Banner::getAll());
        return View::fetch();
    }
   
}
